<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.staticfile.org/vue/3.0.5/vue.global.js"></script>
</head>

<body>
    <div id="app" class="demo">
        <two>
        </two>
    </div>

    <template id="one">
        one----------姓名：{{name}} ,年龄：{{age}}
        <div style="color: blue;">哈哈哈哈哈哈哈哈哈</div>
    </template>
    <template id="two">
        <hr>
        <one></one>
        <hr>
        two----------姓名：{{name}} ,年龄：{{age}}
        <div style="color: red;">嘿嘿嘿嘿嘿嘿嘿嘿嘿</div>
    </template>
</body>
<script>

    const oneConfig = {
        template: '#one',
        setup() {
            // inject 注入依赖
            const name = Vue.inject('name')
            const age = Vue.inject('age')
            console.log(name, age.value)
            return{
                name,
                age
            }
        }
    };
    const twoConfig = {
        template: '#two',
        components: {
            one: oneConfig,
        },
        setup() {
            // inject 注入依赖
            const name = Vue.inject('name')
            const age = Vue.inject('age')
            console.log(name, age.value)
            return{
                name,
                age
            }
        }
    };

    const HelloVueApp = {
        setup() {
            // 在 composition API 中我们可以引入 provide 和 inject 为子孙组件提供数据
            // 提供普通数据
            const name = 'zy'
            Vue.provide('name', name)
            // 提供响应式数据
            const age = Vue.ref(22)
            Vue.provide('age', age)

        },
        components: {
            two: twoConfig
        }
    }
    Vue.createApp(HelloVueApp).mount('#app')
</script>

</html>